package com.kaisu.blog.web.blog.controller;

import com.kaisu.blog.application.blog.BlogApplication;
import com.kaisu.blog.application.blog.dto.*;
import com.kaisu.blog.web.blog.convert.BlogWebConvert;
import com.kaisu.blog.web.blog.request.*;
import com.kaisu.blog.web.blog.vo.BlogPageVO;
import com.kaisu.blog.web.blog.vo.BlogVO;
import com.kaisu.blog.web.blog.vo.CommentPageVO;
import com.kaisu.blog.web.convert.PageRequestConvert;
import com.kaisu.blog.web.request.BatchIdRequest;
import com.kaisu.blog.web.result.Result;
import com.kaisu.blog.web.result.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @author kaisu
 */

@Tag(name = "blog", description = "博客")
@RestController
@RequestMapping("/api/v1/blog")
@RequiredArgsConstructor
public class BlogController {
    private final BlogWebConvert webConvert;
    private final BlogApplication blogApplication;

    @Operation(summary = "创建")
    @PostMapping("")
    public void blogCreate(@RequestBody BlogCreateRequest request) {
        blogApplication.create(new BlogCreateDTO(request.title(), request.content()));
    }

    @Operation(summary = "更新标题")
    @PostMapping("/update/title/{id}")
    public void blogUpdateTitle(@PathVariable String id, @RequestBody BlogUpdateTitleRequest request) {
        blogApplication.updateTitle(id, request.title());
    }

    @Operation(summary = "更新内容")
    @PostMapping("update/content/{id}")
    public void blogEditContent(@PathVariable String id, @RequestBody BlogUpdateContentRequest request) {
        blogApplication.editContent(id, request.content());
    }

    @Operation(summary = "批量删除")
    @PostMapping("batch/delete")
    public void blogDeleteByIds(@RequestBody BatchIdRequest request) {
        blogApplication.deleteByIds(request.getIds());
    }

    @Operation(summary = "发布")
    @PostMapping("publish/{id}")
    public void blogPublish(@PathVariable String id) {
        blogApplication.publish(id);
    }

    @Operation(summary = "详情")
    @GetMapping("byId/{id}")
    public Result<BlogVO> blogById(@PathVariable String id) {
        return Result.success(webConvert.toVO(blogApplication.findById(id)));
    }

    @Operation(summary = "点赞")
    @PostMapping("like/{blogId}")
    public void blogLike(@PathVariable String blogId) {
        blogApplication.like(blogId);
    }

    @Operation(summary = "取消点赞")
    @PostMapping("unlike/{blogId}")
    public void blogUnLike(@PathVariable String blogId) {
        blogApplication.unlike(blogId);
    }

    @Operation(summary = "添加评论")
    @PostMapping("/append/comment")
    public void blogAppendComment(@RequestBody BlogAppendCommentRequest request) {
        blogApplication.addComment(request.getBlogId(), request.getContent());
    }

    @Operation(summary = "获取评论")
    @PostMapping("/page/comment")
    public Result<PageVO<CommentPageVO>> blogComments(@RequestBody CommentPageRequest request) {
        Page<CommentPageDTO> page = blogApplication.commentPage(new CommentPageRequestDTO(request.getBlogId(), PageRequestConvert.convert(request.getPage())));
        return Result.success(PageVO.of(page.map(webConvert::toPageVO)));
    }

    @Operation(summary = "根据用户id查询博客分页")
    @PostMapping("/page/user")
    public Result<PageVO<BlogPageVO>> blogPageByUser(@RequestBody BlogUserPageRequest request) {
        Page<BlogPageDTO> page = blogApplication.pageByUserId(new BlogPageUserRequestDTO(request.getUserId(), PageRequestConvert.convert(request.getPage())));
        return Result.success(PageVO.of(page.map(webConvert::toPageVO)));
    }

    @Operation(summary = "推荐")
    @PostMapping("recommend")
    public Result<PageVO<BlogPageVO>> blogRecommend(@RequestBody BlogRecommendPageRequest request) {
        Page<BlogPageDTO> page = blogApplication.blogRecommend(PageRequestConvert.convert(request.getPage()));
        return Result.success(PageVO.of(page.map(webConvert::toPageVO)));
    }

    @Operation(summary = "我的")
    @PostMapping("my")
    public Result<PageVO<BlogPageVO>> blogMyPage(@RequestBody BlogMyPageRequest request) {
        Page<BlogPageDTO> page = blogApplication.myBlogPage(PageRequestConvert.convert(request.getPage()));
        return Result.success(PageVO.of(page.map(webConvert::toPageVO)));
    }

    @Operation(summary = "关注")
    @PostMapping("followings")
    public Result<PageVO<BlogPageVO>> blogFollowings(@RequestBody BlogFollowingsPageRequest request) {
        Page<BlogPageDTO> page = blogApplication.blogFollowings(PageRequestConvert.convert(request.getPage()));
        return Result.success(PageVO.of(page.map(webConvert::toPageVO)));
    }

    @Operation(summary = "搜索")
    @PostMapping("search")
    public Result<PageVO<BlogPageVO>> blogPageBySearch(@RequestBody BlogSearchPageRequest request) {
        Page<BlogPageDTO> page = blogApplication.blogPageBySearch(new BlogPageSearchRequestDTO(request.getKey(), PageRequestConvert.convert(request.getPage())));
        return Result.success(PageVO.of(page.map(webConvert::toPageVO)));
    }
}